/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.service.resource;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.bean.ResBean;
import com.sinodata.bsm.center.cache.PropertyCache;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.message.ProbeMessenger;
import com.sinodata.bsm.common.exception.AppException;
import com.sinodata.bsm.common.vo.Property;
import com.sinodata.bsm.common.vo.Res;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author fanshengchao
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-9 下午04:19:49          fanshengchao        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class PropertyService {

    @Autowired
    private BaseDao baseDao;
    @Autowired
    private PropertyCache propertyCache;

    public List<Property> findPropertyByResId(Long resId) {
        return propertyCache.getByResId(resId);
    }

    public Property getProperty(Long id) {
        return propertyCache.get(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void saveOrUpdateProperty(Property property) throws AppException {
        if (property.getResId() == null) {
            throw new AppException("属性定自义的resId值为空");
        }
        if (property.getResType() != null) {
            throw new AppException("属性自定义的resType值不为 NULL");
        }

        baseDao.saveOrUpdate(property);
        propertyCache.put(property);
        ProbeMessenger.notifyPropertyAdd(property);

    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deletePropertyById(Long id) throws AppException {
        Property property = propertyCache.get(id);
        if (property == null) {
            return;
        }
        if (property.getResId() == null) {
            throw new AppException("属性定自义的resId值为空");
        }
        if (property.getResType() != null) {
            throw new AppException("属性自定义的resType值不为 NULL");
        }

        propertyCache.remove(property.getId());
        baseDao.delete(property);
    }
}
